<?php

//小站选项应用模型Vote模块
class Vote_model extends Model {

	function Vote_model()
	{
		parent::Model();	
		$this->load->database();
		$this->load->model("plugins_model", "pm");
		$this->load->model("comment_model");
		$this->load->model("options_model");
	}
	
	//获取列表
	function listitem($offset = 0, $pagecount = 20, $order = "id", $desc = "desc")
	{
		$items = array();
		$query = $this->db->from("module_vote")->limit($pagecount, $offset)->order_by($order, $desc)->get();
		if ($query->num_rows()>0) {
			$items = $query->result_array();
			foreach($items as $k => $list){
				$siteid = $this->pm->get_siteid($list["plid"]);
				$site = $this->sm->get($siteid);
				if($site) {
					$items[$k]["domain"] = $site["domain"];
					$items[$k]["sitename"] = $site["name"];
					$items[$k]["logo"] = $site["logo"];
				} else {
					$items[$k]["domain"] = $items[$k]["sitename"] = $items[$k]["logo"] = "";
				}
				
				//用户信息
				$userid = $list["userid"];
				$userinfo = $this->am->get($userid);
				$items[$k]["userinfo"] = $userinfo;
			}
		}
		return $items;
	}
	
	//计算总数
	function countitems()
	{
		return $this->db->count_all_results("module_vote");
	}
	
	//添加
	function add($plid)
	{
		//投票基本信息
		$data = array(
			"title" => p("title"),
			"typeid" => p("typeid"),
			"userid" => $this->userid,
			"plid" => $plid,
			"addtime" => time(),
		);
		$result = $this->db->insert("module_vote", $data);
		$voteid = $this->db->insert_id();
		
		//处理投票选项
		$options = p("option");
		for ($i=0; $i<count($options); $i++) {
			if(trim($options[$i] != "")) {
				$data  = array(
					"voteid" => $voteid,
					"option" => $options[$i],
				);
				$result = $this->db->insert("module_vote_info", $data);
			}
		}
		return $voteid;
	}
	
	//投票
	function post($dataid)
	{
		$typeid = p("typeid");
		$val = p("option_".$dataid);
		if(is_string($val)) $val = array($val);
		foreach ($val as $v) {
			$data = array(
				"voteid" => $dataid,
				"optionid" => $v,
				"userid" => $this->userid,
				"votetime" => time(),
			);
			$result = $this->db->insert("module_vote_val", $data);
		}
		return $result;
	}
	
	//编辑
	function edit($plid)
	{
		$voteid = p("id");
		//投票基本信息
		$data = array(
			"title" => p("title"),
			"typeid" => p("typeid"),
			"userid" => $this->userid,
			"plid" => $plid,
			"addtime" => time(),
		);
		$this->db->where("id", $voteid);
		$result = $this->db->update("module_vote", $data);
//		echo $this->db->last_query();exit;
		
		//删除旧选项
		$result = $this->db->delete("module_vote_info", array("voteid"=>$voteid));		

		//处理投票选项
		if ($result) {
			$options = p("option");
			for ($i=0; $i<count($options); $i++) {
				if(trim($options[$i] != "")) {
					$data  = array(
						"voteid" => $voteid,
						"option" => $options[$i],
					);
					$result = $this->db->insert("module_vote_info", $data);
				}
			}
		}
		return $voteid;
	}
	
	//删除
	function delete($dataid)
	{
		$this->db->delete("module_vote", array("id"=>$dataid));
		$result = $this->db->delete("module_vote_info", array("voteid"=>$dataid));
		return $result;
	}
	
	//删除选择
	function delselected($delstr)
	{
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$delid[]= $_delid[$i];
			}
		}
		if(count($delid) == 0) return false;
		//删除评论
//		$this->comment_model->forum_delete($id);
		$this->db->where_in("dataid", $delid);
		$this->db->delete("comment");
		
		$this->db->where_in("id", $delid);
		$this->db->delete("module_vote");
		
		$this->db->where_in("voteid", $delid);
		$result = $this->db->delete("module_vote_info");
		return $result;
	}
	
	//#获取列表
	function lists($plid, $limit = 5, $offset = 0)
	{
		$lists = array();
		$query = $this->db->from("module_vote")->where(array("plid"=>$plid))->order_by("id","desc")->limit($limit, $offset)->get();
		if($query->num_rows()>0) {
			$lists = $query->result_array();
			foreach ($lists as $k => $row) {
				//模块ID
				$plid = $row["plid"];
				$r = $this->pm->get($plid);
				$lists[$k]["moduleid"] = $r["moduleid"];
				//评论总数
				$opid = $r["opid"];
				$r = $this->options_model->get($opid);
				$siteid = $r["siteid"];
				$commentcount = $this->comment_model->count_lists($siteid, $plid, $row["id"]);
				$lists[$k]["commentcount"] = $commentcount;
				//获取选项
				$voteid = $row["id"];
//				$q = $this->db->get_where("module_vote_info", array("voteid"=>$voteid));
				$q = $this->db->from("module_vote_info")->where(array("voteid"=>$voteid))->order_by("id")->get();
				$options = $q->result_array();
				$lists[$k]["options"] = $options;
				//选项类型
				if($row['typeid'] == 0){
					$type = "单选";
					$box = "radio";
				} else {
					$type = "多选";
					$box = "checkbox";
				}
				$lists[$k]["type"] = $type;
				$lists[$k]["box"] = $box;
				//获取总票数
				$total = $this->get_total($voteid);
				//检测是否已投过
				$isposted = $this->is_posted($row["id"], $this->userid);
				$optionsdata = array();
				if ($isposted) {//获取投票选项比例
					$optionsdata = $this->get_options_data($row["id"]);
				}
				$lists[$k]["isposted"] = $isposted;	
				$lists[$k]["optionsdata"] = $optionsdata;	
				$lists[$k]["total"] = $total;
			}
		}
		return $lists;
	}
	
	//计算总数
	function count_lists($plid)
	{
		$query = $this->db->from("module_vote")->where(array("plid"=>$plid))->get();
		return $query->num_rows();
	}
	
	//#查看内容
	function show($id)
	{
		$row = array();
		$query = $this->db->get_where("module_vote", array("id"=>$id));
		if($query->num_rows()>0) {
			$row = $query->row_array();
			//获取选项
			$voteid = $row["id"];
//			$q = $this->db->get_where("module_vote_info", array("voteid"=>$voteid));
			$q = $this->db->from("module_vote_info")->where(array("voteid"=>$voteid))->order_by("id")->get();
			$options = $q->result_array();
			$row["options"] = $options;
			//选项类型
			if($row['typeid'] == 0){
				$type = "单选";
				$box = "radio";
			} else {
				$type = "多选";
				$box = "checkbox";
			}
			$row["type"] = $type;
			$row["box"] = $box;
			//获取总票数
			$total = $this->get_total($voteid);
			if(isset($this->userid)) {
				//检测是否已投过
				$isposted = $this->is_posted($id, $this->userid);
				$optionsdata = array();
				if ($isposted) {//获取投票选项比例
					$optionsdata = $this->get_options_data($id);
				}
				$row["isposted"] = $isposted;	
				$row["total"] = $total;
			} else {
				$optionsdata = $this->get_options_data($id);
			}
			$row["optionsdata"] = $optionsdata;	
			
			//评论总数
			$r = $this->pm->get($row['plid']);
			$opid = $r["opid"];
			$r = $this->options_model->get($opid);
			$siteid = $r["siteid"];
			$commentcount = $this->comment_model->count_lists($siteid, $row['plid'], $row["id"]);
			$row["commentcount"] = $commentcount;
			$site = $this->sm->get($siteid);
			if($site) {
				$row["domain"] = $site["domain"];
				$row["sitename"] = $site["name"];
				$row["logo"] = $site["logo"];
				$row["siteid"] = $siteid;
			} else {
				$row["domain"] = $row["sitename"] = $row["logo"] = "";
			}

			//用户信息
			$userid = $row["userid"];
			$userinfo = $this->am->get($userid);
			$row["userinfo"] = $userinfo;
		}
		return $row;
	}
	
	//#查看内容
	function get_info($id)
	{
		$row = array();
		$query = $this->db->get_where("module_vote", array("id"=>$id));
		if($query->num_rows()>0) {
			$row = $query->row_array();
			//获取选项
			$voteid = $row["id"];
			$q = $this->db->get_where("module_vote_info", array("voteid"=>$voteid));
			$options = $q->result_array();
			$row["options"] = $options;
			//选项类型
			if($row['typeid'] == 0){
				$type = "单选";
				$box = "radio";
			} else {
				$type = "多选";
				$box = "checkbox";
			}
			$row["type"] = $type;
			$row["box"] = $box;
			//检测是否已投过
			$isposted = $this->is_posted($id, $this->userid);
			$optionsdata = array();
			if ($isposted) {//获取投票选项比例
				$optionsdata = $this->get_options_data($id);
			}
			$row["isposted"] = $isposted;	
			$row["optionsdata"] = $optionsdata;		
		}
		return $row;
	}
	
	//检测是否已投过
	function is_posted($voteid, $userid)
	{
		$query = $this->db->get_where("module_vote_val", array("voteid"=>$voteid, "userid"=>$userid));
		if ($query->num_rows()>0) {
			return true;
		}
		return false;
	}
	
	//获取投票选项比例
	function get_options_data($voteid)
	{
		//获取总票数
		$total = $this->get_total($voteid);
		
		//获取所有选项
		$voteinfo = $this->get_voteinfo($voteid);
		foreach ($voteinfo as $k => $vote) {
			//获取投票选项数量
			$query = $this->db->get_where("module_vote_val", array("voteid"=>$voteid, "optionid"=>$vote['id']));
			$count =  $query->num_rows();
			$voteinfo[$k]["count"] = $count;
			$voteinfo[$k]["scale"] = sprintf("%01.2f",$count/$total)*100;
		}
		return $voteinfo;
	}
	
	//获取总票数
	function get_total($voteid)
	{
		$query = $this->db->get_where("module_vote_val", array("voteid"=>$voteid));
		return $query->num_rows();
	}
	
	//获取所有选项
	function get_voteinfo($voteid)
	{
		$query = $this->db->from("module_vote_info")->where(array("voteid"=>$voteid))->order_by("id")->get();
		return $query->result_array();
	}
}

/* End of file */